var BTO = {};
/*
 * add jiangbo
 * date: 2010.4.20
 */
/*用于表单元素的处理
 * create: jiangbo
 * date : 2010.01.19
 */
BTO.FORM = {
    init: (function(oForm){
        //为避免重复在表单DOM中遍历，所以有此事件，处理各种情况而只需遍历一次DOM
        var focusFirstInput = 0;
        if (!oForm)
            return false;
        var oFormElements;
        aFormElements = oForm.elements;
        for (i = 0; i < aFormElements.length; i++) {
            var aElementTagName = aFormElements[i].tagName;
            var aElementType = aFormElements[i].type.toUpperCase();

            //MaxValue
            BTO.FORM.input.setMaxValue(aFormElements[i]);

            //FocusFirstInput
            if (!focusFirstInput) {
                if ((aFormElementsType == 'TEXT' || 'PASSWORD') && !aFormElements[i].disabled) {
                    aFormElements[i].focus();
                    focusFirstInput = 1;
                }
            }

            //initButtonHover
            if (aElementTagName == 'BUTTON' || aElementType == ('BUTTON' || 'SUBMIT' || 'RESET')) {
                if ((aFormElements[i].parentNode.tagName == 'SPAN' && aFormElements[i].parentNode.className.indexOf('btn_normal') != -1)) {
                    if (aFormElements[i].onmouseout) aFormElements[i].removeAttribute('onmouseout');
                    if (aFormElements[i].onmousemove) aFormElements[i].removeAttribute('onmousemove');
                    aFormElements[i].className = '';
                    var outerSPAN = document.createElement('span');
                    outerSPAN.className = 'btn_normal';
                    aFormElements[i].parentNode.insertBefore(outerSPAN, aFormElements[i]);
                    outerSPAN.appendChild(aFormElements[i]);
                }
                if (!isIEBrowser) {
                    aFormElements[i].setAttribute('onclick', aFormElements[i].getAttribute('onclick') + '; return false;');
                }
            }

            //initTextFeild
            if (aElementType == ('TEXT' || 'PASSWORD')) {
                aFormElements[i].className = '';
                if (aFormElements[i].parentNode.className && aFormElements[i].parentNode.className.indexOf('inputbox') != -1) {
                    aFormElements[i].parentNode.removeAttribute('class');
                }
                if (!(aFormElements[i].parentNode.className == 'text_normal_in' && aFormElements[i].parentNode.tagName == 'SPAN')) {
                    var inputSpanOut = document.createElement('span');
                    var inputSpanIn = document.createElement('span');
                    inputSpanOut.className = 'text_normal_out';
                    inputSpanIn.className = 'text_normal_in';
                    inputSpanOut.appendChild(inputSpanIn);
                    aFormElements[i].parentNode.insertBefore(inputSpanOut, aFormElements[i]);
                    inputSpanIn.appendChild(aFormElements[i]);
                }
            }
        }
    }),
    focusFirstInput: (function(oForm){
        if (!oForm)
            return false;
        var aFormElements;
        aFormElements = oForm.elements;
        for (i = 0; i < aFormElements.length; i++) {
            if ((aFormElements[i].type.toUpperCase() == 'TEXT' || 'PASSWORD') && !aFormElements[i].disabled) {
                //如果i元素为TEXT或者PASSWORD类型，并且其没有被禁用，则focus到此元素
                aFormElements[i].focus();
                return;
            }
        }
    }),
    input: {
        setMaxValue: function(e){
            //最大值
            var e = e || window.event;
            var oObj = e.srcElement ? e.srcElement : e.target;

            if (oObj.getAttribute('maxValue')) {
                //存在maxValue时，同时也过滤其值为数字型
                oObj.value = oObj.value.replace(/[^0-9]/g, '');

                var maxValue = oObj.getAttribute('maxValue') * 1;
                var oObjValue = oObj.value * 1;
                //超出最大值时重设为最大值
                if (oObjValue > maxValue) {
                    oObj.value = maxValue;
                }
            } else {
                return false;
            }
        }
        /*enterToNext : (function(oObj){
         //回车跳到下一个表单元素
         if (!oObj) {
         return false;
         }
         var oForm = oObj.form;
         var aFormElements = oForm.elements;
         var oObjIndex = oObj.getAttribute('formIndex') || function(){
         for (i = aFormElements.length - 1; i >= 0; i--) {
         if (aFormElements[i] == oObj) {
         $(oObj).setAttribute('formIndex',i);
         return i;
         }
         }
         }();
         //如果为回车键则跳转到下一个input元素
         //aFormElements[oObjIndex+1].focus();
         }),*/
    }
};

/* 成生与游戏外观一致的select样式
 * addd by xijiangbo 2010.05.14
 * modify in 2010.05.17
 */
/*
 * add jiangbo
 * date: 2010.5.14
 * 用于处理滚动框内的select元素
 * 日后将可能取代原有的loadselect函数
 */
BTO.FORM.select = {
    oObj: '',
    onceEvent: 0,
    init: function(oObj){
        //初始化，重写样式，绑定函数
        var width, tObjText;
        width = oObj.offsetWidth;
		var nSelectIndex = 0;
		for (var i = oObj.options.length - 1; i >= 0; i--){
			if (oObj.options[i].selected){
				nSelectIndex = i;
			}
		}
		tObjText = oObj.options[nSelectIndex].innerHTML;

        //外层套入两个span模拟select的外观效果
        var selectIn, selectOut, textNode;
        selectIn = document.createElement('span');
        selectOut = document.createElement('span');
        textNode = document.createElement('span');
        selectOut.className = 'selectOut';
        selectIn.className = 'selectIn';
        selectIn.style.width = width - 25 + 'px';
        selectIn.style.padding = '0 24px 0 6px';
        textNode.innerHTML = tObjText;
        selectIn.appendChild(textNode);
        selectOut.appendChild(selectIn);
        oObj.parentNode.insertBefore(selectOut, oObj);
        selectIn.appendChild(oObj);
        oObj.className = 'noShow';
        //oObj.replaceNode(selectIn.appendChild(oObj));
        //生成一个名为oObj.id + 'List'的ul列表
        var oDiv = document.createElement('div');
        oDiv.id = 'ulListFor' + oObj.id;
        oDiv.className = 'selectUlList';
        oDiv.style.width = width - 15 + 'px';
		if (is_ie) {
			oDiv.style.height = '100px';
		} else {
			oDiv.style.height = '130px';
		}

		var oUl = document.createElement('ul');
        var nOptionLength = oObj.options.length;
        for (var i = 0; i < nOptionLength; i++) {
            var oLi = document.createElement('li');
            oLi.setAttribute("onmouseover", "this.className = 'active'");
            oLi.setAttribute("onmouseout", "this.className = ''");
            //oLi.setAttribute('value', oObj.options[i].value);
            oLi.appendChild(document.createTextNode(oObj.options[i].innerHTML));
            oUl.appendChild(oLi);
			BTO.EVENT.addEvent('click', this.liClick, oLi);
        }
        oDiv.appendChild(oUl);

        //追加此ulList到页面中
        //selectOut.parentNode.appendChild(oDiv);
		$('host_right').appendChild(oDiv);

        //为其绑定onclick事件
        selectOut.onclick = this.click;

		//在document对像上绑定mousedown和scroll事件，以实现点击其它区域时select消失
        if (!this.onceEvent) {
            BTO.EVENT.addEvent('mousedown', this.hideList);
            BTO.EVENT.addEvent('scroll', this.hideList);
            this.onceEvent = 1;
        }
    },
    click: function(){
        //模拟select的点击事件
        var select = this.getElementsByTagName('select')[0];
        var popList = $('ulListFor' + select.id);
        if (popList.style.display == '') {
            var absOffset = getAbsPos(this,$('host_right'));
			popList.style.top = absOffset.y + this.offsetHeight - 1 + 'px';
        	popList.style.left = absOffset.x + 1 + 'px';
			popList.style.zIndex = '99';
            popList.style.display = 'block';
        } else {
            popList.style.display = '';
        }
    },
    liClick: function(e){
        //列表项的点击事件，返回值到select中
        var e = e || window.event;
        var obj = e.srcElement || e.target;
        var oDiv = obj.parentNode.parentNode;
        var oSelect = $(oDiv.id.replace('ulListFor', ''));
        oOption = oSelect.options;
        for (var i = oOption.length - 1; i >= 0; i--) {
            if (obj.innerHTML == oOption[i].innerHTML) {
                //设置选中项
                oOption[i].setAttribute('selected', 1);
                //设置selectIn中的文字内容
                oSelect.parentNode.getElementsByTagName('span')[0].innerHTML = oOption[i].innerHTML;
                oDiv.style.display = '';
            } else {
                oOption[i].removeAttribute('selected');
            }
        }
    },
    hideList: function(e){
        //在列表外位置点击或者滚动页面时，隐藏列表
        var e = e || window.event;
        var obj = e.srcElement || e.target;
        var clickInner = 0;
        if (obj.parentNode) {
            while (obj.parentNode) {
                if (obj.className == 'selectUlList') {
                    clickInner = 1;
                    break;
                }
                obj = obj.parentNode;
            }
        }
        if (!clickInner) {
            if (document.getElementsByClassName) {
                var aDiv = document.getElementsByClassName('selectUlList');
                for (var i = aDiv.length - 1; i >= 0; i--) {
                    aDiv[i].style.display = '';
                }
            } else {
                var aDiv = document.getElementsByTagName('div');
                for (var i = aDiv.length - 1; i >= 0; i--) {
                    if (aDiv[i].id.indexOf('ulListFor') != -1) {
                        aDiv[i].style.display = '';
                    }
                }
            }
        }
    },
    callback: function(){

    }
};


/*BTO.FORM.select = function(oObj){
    this.oObj = oObj; //select
    this.outerSPAN;		//selectIn
    this.width = oObj.offsetWidth; //select宽度
    this.tObjText; //select中显示的文字
    this.relUL; //与select相关联的ulList列表id
    var that = this;
    if (typeof BTO.FORM.select._initialized == 'undefined') {

        BTO.FORM.select.prototype.init = function(){
            var nSelectIndex = 0;
            for (var i = this.oObj.options.length - 1; i >= 0; i--) {
                if (this.oObj.options[i].selected) {
                    nSelectIndex = i;
                }
            }
            this.tObjText = this.oObj.options[nSelectIndex].innerHTML;

            //外层套入两个span模拟select的外观效果
            var selectIn, selectOut, textNode;
            selectIn = document.createElement('span');
            selectOut = document.createElement('span');
            textNode = document.createElement('span');
            selectOut.className = 'selectOut';
            selectIn.className = 'selectIn';
            selectIn.style.width = this.width - 25 + 'px';
            selectIn.style.padding = '0 24px 0 6px';
            textNode.innerHTML = this.tObjText;
            selectIn.appendChild(textNode);
            selectOut.appendChild(selectIn);
            this.oObj.parentNode.insertBefore(selectOut, this.oObj);
            selectIn.appendChild(this.oObj);
			this.oObj.className='noShow';
			this.outerSPAN = selectIn;

            //oObj.replaceNode(selectIn.appendChild(oObj));
            //生成一个名为oObj.id + 'List'的ul列表
            var oDiv = document.createElement('div');
            oDiv.id = 'ulListFor' + this.oObj.id;
            oDiv.className = 'selectUlList';
            oDiv.style.width = this.width - 5 + 'px';
            oDiv.style.height = '110px';

            var oUl = document.createElement('ul');
            var nOptionLength = oObj.options.length;
            for (var i = 0; i < nOptionLength; i++) {
                var oLi = document.createElement('li');
                oLi.setAttribute("onmouseover", "this.className = 'active'");
                oLi.setAttribute("onmouseout", "this.className = ''");
                //oLi.setAttribute('value', oObj.options[i].value);
                oLi.appendChild(document.createTextNode(this.oObj.options[i].innerHTML));
                oUl.appendChild(oLi);
                BTO.EVENT.addEvent('click', this.liClick(), oLi);
            }
            oDiv.appendChild(oUl);

            //追加此ulList到页面中
            //selectOut.parentNode.appendChild(oDiv);
			$('host_right').appendChild(oDiv);

            this.relUL = oDiv; //与select相关联的ulList列表id
            //为其绑定onclick事件
            BTO.EVENT.addEvent('click', this.click(), selectOut);

            //在document对像上绑定mousedown和scroll事件，以实现点击其它区域时select消失
            if (!this.onceEvent) {
                BTO.EVENT.addEvent('mousedown', that.hideList);
                BTO.EVENT.addEvent('scroll', that.hideList);
            }
        };

        BTO.FORM.select.prototype.click = function(){
            //模拟select的点击事件
            var that = this;
            return function(){
                if (that.relUL.style.display == '') {
                    var absOffset = getAbsPos(that.outerSPAN,$('host_right'));
                    that.relUL.style.top = absOffset.y + 23 + 'px';
                    that.relUL.style.left = absOffset.x - 1 + 'px';
                    that.relUL.style.zIndex = '99';
                    that.relUL.style.display = 'block';
                } else {
                    that.relUL.style.display = '';
                }
            }
        };

        BTO.FORM.select.prototype.liClick = function(){
            //列表项的点击事件，返回值到select中
            var that = this;
            return function(e){
                var e = e || window.event;
                var obj = e.srcElement || e.target;

                var oOption = that.oObj.options;
                for (var i = oOption.length - 1; i >= 0; i--) {
                    if (obj.innerHTML == oOption[i].innerHTML) {
                        //设置选中项
                        oOption[i].setAttribute('selected', 1);
                        //设置selectIn中的文字内容
                        obj.innerHTML = that.oObj.parentNode.getElementsByTagName('span')[0].innerHTML = oOption[i].innerHTML;
                        that.relUL.style.display = '';
                    } else {
                        oOption[i].removeAttribute('selected');
                    }
                }
            }
        };

        BTO.FORM.select.prototype.hideList = function(e){
            //在列表外位置点击或者滚动页面时，隐藏列表
            var e = e || window.event;
            var obj = e.srcElement || e.target;

            var clickInner = 0; //是否点击在内部
            if (obj.parentNode) {
                while (obj.parentNode) {
                    if (obj.className == 'selectUlList') {
                        clickInner = 1;
                        break;
                    }
                    obj = obj.parentNode;
                }
            }
            if (!clickInner) {
                if (document.getElementsByClassName) {
                    //DOM5 方法（ff/other）
                    var aDiv = document.getElementsByClassName('selectUlList');
                    for (var i = aDiv.length - 1; i >= 0; i--) {
                        aDiv[i].style.display = '';
                    }
                } else {
                    //ie
                    var aDiv = document.getElementsByTagName('div');
                    for (var i = aDiv.length - 1; i >= 0; i--) {
                        if (aDiv[i].id.indexOf('ulListFor') != -1) {
                            aDiv[i].style.display = '';
                        }
                    }
                }
            }
        };

        (function(){
            //在document对像上绑定mousedown和scroll事件，以实现点击其它区域时select消失，只需绑定一次
            BTO.EVENT.addEvent('mousedown', that.hideList);
            BTO.EVENT.addEvent('scroll', that.hideList);
        })();

        BTO.FORM.select._initialized = 1;
    }
    this.init();
}*/

/* 事件处理器
 * add by jiangbo
 * 2010.01.19
 */
BTO.EVENT = {
    supportDOM: document.addEventListener ? 1 : (document.attachEvent ? 0 : -1),
    //supportDOM : 1: ff/webkit/opera...(标准2级DOM支持)			0:ie 5.5+			-1:不支持高级DOM事件模型

    formatEvent: function(oEvent){
        //格式化ie的事件模型为DOM标准类型
        if (BTO.BROWSE.OS == 'windows' && BTO.BROWSE.app == 'ie') {
            oEvent.charCode = (oEvent.type == 'keypress') ? oEvent.keyCode : 0;
            oEvent.eventPhase = 2; //在冒泡阶段捕捉事件
            oEvent.isChar = (oEvent.charCode > 0); //charCode大于0为真
            oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
            oEvent.pageY = oEvent.clientY + document.body.scrollTop;
            oEvent.preventDefault = function(){
                this.returnValue = false;
            }
            if (oEvent.type == 'mouseout') {
                oEvent.relatedTarget = oEvent.toElement;
            } else if (oEvent.type == 'mouseover') {
                oEvent.relatedTarget = oEvent.formElement;
            }

            oEvent.stopPropagation = function(){
                this.cancelBubble = true;
            }

            oEvent.target = oEvent.srcElement;
            oEvent.time = (new Date()).getTime();
        }
        return oEvent;
    },
    addEvent: function(eEvent, fFun, oObj){
        //增加事件，省略oObj参数时事件将绑定到 document对像上
        //eEvent前省略on    如 需绑定onclick事件，则写为'click'
        if (!oObj) {
            oObj = document;
        }

        if (BTO.EVENT.supportDOM == 1) {
            oObj.addEventListener(eEvent, fFun, true);
        } else if (BTO.EVENT.supportDOM == 0) {
            oObj.attachEvent('on' + eEvent, fFun);
        } else if (BTO.EVENT.supportDOM == -1) {
            return false;
            //alert ("对不起，您的浏览器或设备不支持某些高级JavaScript特性、或者JavaScript支持被关闭，请用更高级别的浏览器或其它设备登录本游戏");
        }
    },
    removeEvent: function(eEvent, fFun, oObj){
        //移除事件
        if (!oObj) {
            oObj = document;
        }
        //if (!$(oObj)) return false;

        if (BTO.EVENT.supportDOM == 1) {
            oObj.removeEventListener(eEvent, fFun, true);
        } else if (BTO.EVENT.supportDOM == 0) {
            oObj.detachEvent('on' + eEvent, fFun);
        } else if (BTO.EVENT.supportDOM == -1) {
            return false;
            //alert ("对不起，您的浏览器或设备不支持某些高级JavaScript特性、或者JavaScript支持被关闭，请用更高级别的浏览器或其它设备登录本游戏");
        }
    }
};

BTO.CLIENT = function(){
    var client = {};
    var userAgent = navigator.userAgent.toLowerCase();
    client.browse = function(){
        if (/mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)) {
            return 'firefox';
        } else if (/webkit/.test(userAgent)) {
            return 'safari';
        } else if (/opera/.test(userAgent)) {
            return 'opear';
        } else if (/msie/.test(userAgent) && !/opera/.test(userAgent)) {
            return 'ie';
        } else {
            return 'other';
        }
    }();
    client.OS = function(){
        switch (navigator.platform) {
            case 'Win32' || 'Windows':
                return 'Win';
            case 'Mac68K' || 'MacPPC' || 'Macintosh':
                return 'Mac';
            case 'X11':
                return 'UNIX';
            default:
                return 'unknow';
        }
    }();
	client.version = function(){
		//
	}();
    client.addFav = function(){
        if (BTO.CLIENT.browse == 'ie') {
            window.external.addFavorite(isNeedFacUrl, isNeedFacText);
        } else {
            window.sidebar.addPanel(isNeedFacText, isNeedFacUrl, '');
        }
    }
    return client;
}();


/* 载入对应的HTML模板，并调用其load事件，生成页面
 * add by xijiangbo 2010.05.16
 */
BTO.TEMPLATES = {
    load: function(oTemplates,subFunctionArguments){
		var subFunctionArguments = subFunctionArguments || null;
        var aJs = document.getElementsByTagName('script'), bTemplateIsLoad = 0;
        for (var i = aJs.length - 1; i >= 0; i--) {
            if (aJs[i].src.indexOf(oTemplates.action) != -1) {
                bTemplateIsLoad = 1;
            }
        }
		//兼容以往的模式
		c.action = oTemplates['action']; //模版名
		c._action_prowse = oTemplates['action_prowse']; //子模块名

        if (!bTemplateIsLoad) {
			//载入对应的js文件写入到html中
			var oJS = document.createElement('script');
			oJS.src = ImgRoot + 'js/templates/' + oTemplates['action'] + '.js';
			oJS.type = 'text/javascript';
			oJS.charset = 'GBK';
			document.getElementsByTagName('head')[0].appendChild(oJS);
			tryToLoadTemplates = window.setInterval(function(){
				if (eval(oTemplates.action) != undefined) {
					var s = (eval(c.action)).load({},subFunctionArguments);
					c.__OpenHostRight();
					$('host_right').innerHTML = s;
					$('host_right').style.display = 'block';
					handleHtmlForFirefox($('host_right'));
					window.clearInterval(tryToLoadTemplates);
				}
			},200);
		} else {
			if (c.action) {
				var s = (eval(c.action)).load({},subFunctionArguments);
				$('host_right').innerHTML = s;
				$('host_right').style.display = 'block';
				handleHtmlForFirefox($('host_right'));
			}
		}
    }
}